package com.mxpio.erp.equipment.controller;

import com.mxpio.erp.common.equipment.entity.EquipmentStock;
import com.mxpio.erp.common.equipment.entity.EquipmentStockLocation;
import com.mxpio.erp.common.equipment.service.EquipmentStockLocationService;
import com.mxpio.erp.common.equipment.service.EquipmentStockService;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.query.Criteria;
import com.mxpioframework.jpa.query.Operator;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import java.util.List;

/**
* @Description: 库位定义
* @Author: MxpIO
* @Date:   2021-10-06
* @Version: V1.0
*/
@Tag(name = "EquipmentStockLocationController", description = "库位定义")
@RestController
@RequestMapping("/erp/equipment/stocklocation/tpmStockLocation")
@Slf4j
public class EquipmentStockLocationController {
   @Autowired
   private EquipmentStockLocationService tpmStockLocationService;
   @Autowired
   private EquipmentStockService stockService;

   /**
    * 分页列表查询
    *
    * @param criteria
    * @param pageNo
    * @param pageSize
    * @param req
    * @return
    */
   @GetMapping(value = "/page")
   @Operation(summary = "库位定义-分页列表查询", description = "库位定义-分页列表查询", method = "GET")
   public Result<?> queryPageList(@RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                  @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                  Criteria criteria,
                                  HttpServletRequest req) {
       Pageable page = PageRequest.of(pageNo-1, pageSize);
       Page<EquipmentStockLocation> pageList = tpmStockLocationService.listPage(EquipmentStockLocation.class,page, criteria);
       return Result.OK(pageList);
   }

    @GetMapping("/list")
    @Operation(summary = "库位定义-分页列表查询", description = "库位定义（无分页）", method = "GET")
    public Result<List<EquipmentStockLocation>> list(@Parameter(description="查询参数") Criteria criteria) throws UnsupportedEncodingException {
        List<EquipmentStockLocation> list = tpmStockLocationService.list(EquipmentStockLocation.class, criteria);
        return Result.OK(list);
    }

   /**
    *   添加
    *
    * @param tpmStockLocation
    * @return
    */
   @PostMapping(value = "/add")
   @Operation(summary = "库位定义-添加", description = "库位定义-添加", method = "POST")
   public Result<?> add(@RequestBody EquipmentStockLocation tpmStockLocation) {
//       EquipmentStock stock = stockService.getById(EquipmentStock.class,tpmStockLocation.getStockId());
//       tpmStockLocation.setFactory(stock.getFactory());
       tpmStockLocationService.save(tpmStockLocation);
       return Result.OK("添加成功！");
   }

   /**
    *  编辑
    *
    * @param tpmStockLocation
    * @return
    */
   @PutMapping(value = "/edit")
   @Operation(summary = "库位定义-编辑", description = "库位定义-编辑", method = "PUT")
   public Result<?> edit(@RequestBody EquipmentStockLocation tpmStockLocation) {
       EquipmentStock stock = stockService.getById(EquipmentStock.class,tpmStockLocation.getStockId());
       tpmStockLocation.setFactory(stock.getFactory());
       tpmStockLocationService.update(tpmStockLocation);
       return Result.OK("编辑成功!");
   }

   /**
    *   通过id删除
    *
    * @param id
    * @return
    */
   @DeleteMapping(value = "/delete")
   @Operation(summary = "库位定义-通过id删除", description = "库位定义-通过id删除", method = "DELETE")
   public Result<?> delete(@RequestParam(name="id",required=true) String id) {
       tpmStockLocationService.remove(EquipmentStockLocation.class,id);
       return Result.OK("删除成功!");
   }

   /**
    *  批量删除
    *
    * @param ids
    * @return
    */
   @DeleteMapping(value = "/deleteBatch")
   @Operation(summary = "库位定义-批量删除", description = "库位定义-批量删除", method = "DELETE")
   public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
       Criteria c = Criteria.create().addCriterion("id", Operator.IN, Arrays.asList(ids.split(",")));
       tpmStockLocationService.removeBatch(EquipmentStockLocation.class,c);
       return Result.OK("批量删除成功!");
   }

   /**
    * 通过id查询
    *
    * @param id
    * @return
    */
   @GetMapping(value = "/queryById")
   @Operation(summary = "库位定义-通过id查询", description = "库位定义-通过id查询", method = "GET")
   public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
       EquipmentStockLocation tpmStockLocation = tpmStockLocationService.getById(EquipmentStockLocation.class,id);
       if(tpmStockLocation==null) {
           return Result.error("未找到对应数据");
       }
       return Result.OK(tpmStockLocation);
   }

  /* *//**
   * 导出excel
   *
   * @param request
   * @param tpmStockLocation
   *//*
   @RequestMapping(value = "/exportXls")
   public ModelAndView exportXls(HttpServletRequest request, EquipmentStockLocation tpmStockLocation) {
       return super.exportXls(request, tpmStockLocation, EquipmentStockLocation.class, "库位定义");
   }

   *//**
     * 通过excel导入数据
   *
   * @param request
   * @param response
   * @return
   *//*
   @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
   public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
       return super.importExcel(request, response, EquipmentStockLocation.class);
   }*/

}
